//
//  RealViewController.m
//  Streaming
//
//  Created by 官琛 on 15-6-27.
//  Copyright (c) 2015年 liuxun. All rights reserved.
//

#import "RealViewController.h"

@interface RealViewController ()

@end

@implementation RealViewController

- (void)dealloc
{
    [self.tableView release];
    [self.array release];
    [self.real release];
    [self.scroll release];
    [self.newsIdArray release];

    
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    [[DateHandel shareHandle]createRealTable];
    self.navigationItem.title = @"热点";
    
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT) style:UITableViewStyleGrouped];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.view addSubview:self.tableView ];
    [self.tableView release];
    [self afnGetRequest];
    [self headerRefesh];
   
    
    if (WIDTH == 320 && HEIGHT == 568) {
        self.aView = [[titleView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, 260)];
    }
    if (WIDTH == 414 && HEIGHT == 736) {
        self.aView = [[titleView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, 320)];
    }
    if (WIDTH == 375 && HEIGHT == 667){
        self.aView = [[titleView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, 260)];
    }

    
    self.aView.myDelegate = self;
    self.tableView.tableHeaderView = self.aView;
    [self.aView  release];
    
    
    
}


-(void)headerRefesh
{
    self.tableView.header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        self.isUpLoading = NO;
        
        self.HUD = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
        self.HUD.labelText = @"正在加载中";
        [self.HUD show:YES];
        [self afnGetRequest];
        
        [self.tableView reloadData];
        [self.HUD hide:YES];
        [self.tableView.header endRefreshing];
        
        if (WIDTH == 320 && HEIGHT == 568) {
            self.aView = [[titleView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, 260)];
        }
        if (WIDTH == 414 && HEIGHT == 736) {
            self.aView = [[titleView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, 320)];
        }
        if (WIDTH == 375 && HEIGHT == 667){
            self.aView = [[titleView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, 260)];
        }
        
        
        self.aView.myDelegate = self;
        self.tableView.tableHeaderView = self.aView;
        [self.aView  release];

        
    }];
    
}


-(void)pushHotPoint:(NSInteger)newsID
{
   RealDetailsViewController *view = [[RealDetailsViewController alloc]init];
    view.newsId = newsID;
    NSLog(@"%ld",(long)view.newsId);
    [self.navigationController  pushViewController:view animated:YES];
    [view release];
    
}


-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellInfer = @"realCell";
    RealTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellInfer];
    if (cell == nil) {
        cell = [[RealTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellInfer];
    }
    Real *real = [self.array objectAtIndex:indexPath.row];
    cell.real = real;
    
    
    return cell;


}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return  self.array.count;
}


-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [self.tableView deselectRowAtIndexPath:indexPath animated:YES];
    RealDetailsViewController *view = [[RealDetailsViewController alloc]init];
    
    view.newsId = [[self.newsIdArray objectAtIndex:indexPath.row]integerValue];
    
    [self.navigationController pushViewController:view animated:YES];
    [view release];
    NSLog(@"%ld",(long)view.newsId);
}


-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 130;
}


-(void)afnGetRequest
{
    self.array = [NSMutableArray array];

    self.array  = [[DateHandel shareHandle]selectAllReal];
    
    
    AFNetworkReachabilityManager *netWorkManager = [AFNetworkReachabilityManager sharedManager];
    NSString *url_string = @"http://api.m.mtime.cn/News/NewsList.api";
    
    
    //[NSSet setWithObjects:@"text/plain",@"text/json",@"application/json",@"text/javascript",@"text/html",nil];  代表支持所有的接口类型
    AFHTTPRequestOperationManager* manager = [AFHTTPRequestOperationManager manager];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/plain",@"text/json",@"application/json",@"text/javascript",@"application/x-javascript",@"text/html",nil];
    
    
    [manager GET:url_string parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        [netWorkManager stopMonitoring];
//    NSLog(@"使用AFN进行get请求 ===  %@",responseObject);
        [[DateHandel shareHandle]dropRealTable];
        [[DateHandel shareHandle]createRealTable];
        [self.array removeAllObjects];
        
        
        NSMutableArray *array = [NSMutableArray array];
        array = [responseObject objectForKey:@"newsList"];
        self.newsIdArray = [NSMutableArray array ];
        for (NSMutableDictionary *dic in array) {
            Real *rea = [[Real alloc]init];
            rea.title = [dic objectForKey:@"title"];
            rea.title2 = [dic objectForKey:@"title2"];
            rea.commentCount = [[dic objectForKey:@"commentCount"]integerValue];
            rea.idd = [[dic objectForKey:@"id"]integerValue];
            rea.type = [[dic objectForKey:@"type"]integerValue];
            rea.image = [dic objectForKey:@"image"];
            [self.array addObject:rea];
            [self.newsIdArray addObject:[NSString stringWithFormat:@"%ld",(long)rea.idd]];
            [[DateHandel shareHandle]insertReal:rea];
            [rea release];
        }
        
        [self.tableView reloadData];
      [self.HUD hide:YES];
        
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
        NSLog(@"失败==== %@",error);
        UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"提示" message:@"网络不给力" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [alert show];
        [alert autorelease];
                [self.HUD hide:YES];
        
        
        
    }];
    
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
